/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pbv.azsg.web;

import java.util.List;

import com.pbv.azsg.domain.FuelEntity;
import com.pbv.azsg.service.IFuelService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;

    

/**
 *
 * @author b.prikhodko
 */
@Controller
public class FuelController {
    
    @Autowired
    private IFuelService fuelSevice;
    
    @RequestMapping(value = "/fuels")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<FuelEntity> getFuels() {               
        return fuelSevice.getFuels();                      
    }

    
    @RequestMapping(value = "/fuel")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public FuelEntity getFuel(Long id) {            
      try {
            return fuelSevice.getFuel( id );                      
      } catch ( ArrayIndexOutOfBoundsException e ) {
          return new FuelEntity( id, "Not Found");
      }
    }

    
    @RequestMapping("/fuels.html")
    public String viewFuels(Model model) {        
        model.addAttribute( getFuels() );
        return "fuelsP";
    }
    
    @RequestMapping("/")
    public String home() {
        return "redirect:/fuels.html";
    }
}
